package com.yycx.module.bus.provider.handler.settle;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.GetInterceptor;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusOrderDetailsService;
import com.yycx.module.bus.provider.service.BusSalemanBankService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 结算单详情扩展
 *
 * @author zyf
 */
@Component("settlePaymentGetHandler")
public class SettlePaymentGetHandler implements GetInterceptor {

    @Autowired
    private BusDeptService deptService;

    @Autowired
    private BusSalemanService salemanService;

    @Autowired
    private BusSalemanBankService salemanBankService;

    @Autowired
    private BusOrderDetailsService orderDetailsService;

    @Override
    public void prepare(CriteriaQuery cq, EntityMap entityMap) {
        cq.select(BusPayment.class, "*");
    }

    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        Long deptId = map.getLong("deptId");
        Long bankId = map.getLong("bankId");
        Long orderDetailsId = map.getLong("orderDetailsId");
        Integer settleType = map.getInt("settleType", 1);
        BusDept dept = deptService.getById(deptId);
        BusSaleman deptSaleMan = salemanService.getById(dept.getSalemanId());
        map.put("deptSaleManName", deptSaleMan.getSaleManName());
        BusSalemanBank salemanBank = salemanBankService.getById(bankId);
        map.put("saleManName", salemanBank.getSaleManName());
        map.put("bankName", salemanBank.getBankName());
        map.put("bankNo", salemanBank.getBankNo());
        BusOrderDetails orderDetails = orderDetailsService.getById(orderDetailsId);
        Integer hasError = 0;
        if (FlymeUtils.isNotEmpty(orderDetails)) {
            BigDecimal amount = map.getBigDecimal("amount");
            BigDecimal settleAmount = orderDetails.getSettleAmount();
            BigDecimal settleAmount2 = orderDetails.getSettleAmount2();
            BigDecimal settleAmount3 = orderDetails.getSettleAmount3();
            if (settleType.equals(1)) {
                if (!amount.equals(settleAmount)) {
                    hasError = 1;
                }
            }
            if (settleType.equals(2)) {
                if (!amount.equals(settleAmount2)) {
                    hasError = 1;
                }
            }
            if (settleType.equals(3)) {
                if (!amount.equals(settleAmount3)) {
                    hasError = 1;
                }
            }
        }
        map.put("hasError", hasError);
    }
}
